home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-10-05 | 5.4 KB | 222 lines | [TEXT/MMCC] |
- /*===================================================================
- MacUtilities.c
-
- ©1991 Greg Anderson
- greggor@apple.com
-
- Misc utility routines
- ===================================================================*/
- #include "MacUtilities.h"
-
- #include <OSUtils.h>
-
- Ptr gApplicStart = nil;
- Ptr gApplicEnd = nil;
- Ptr gSysStart = nil;
- Ptr gSysEnd = nil;
-
- /*----------------------------------------------------------------------
- // PtrLooksValid
- //
- // Return true if the given pointer is even and non-nil
- ----------------------------------------------------------------------*/
- short PtrLooksValid( Ptr ptr )
- {
- return ( (ptr != nil) && ((((long)ptr) & 1) == 0) );
- }
-
- /*----------------------------------------------------------------------
- // PtrValid
- //
- // Return true if the given pointer looks valid and lies within
- // the range of our application zone or the system zone
- ----------------------------------------------------------------------*/
- short PtrValid( Ptr ptr )
- {
- Boolean valid = false;
-
- /*
- // Don't bother with range checking if the pointer doesn't
- // look valid
- */
- if( PtrLooksValid( ptr ) )
- {
- /*
- // If our globals have not been set up, then
- // initialize them
- */
- if( gApplicStart == nil )
- {
- THz sysZone;
- THz applicZone;
-
- sysZone = SystemZone();
- applicZone = ApplicZone();
-
- gApplicStart = (Ptr) applicZone;
- gApplicEnd = (Ptr) (applicZone->bkLim);
-
- gSysStart = (Ptr) sysZone;
- gSysEnd = (Ptr) (sysZone->bkLim);
- }
-
- /*
- // The pointer should lie within either our
- // application or within the System zone.
- // If it does not, we should not be playing
- // with it.
- */
- if( ((ptr > gApplicStart) && (ptr < gApplicEnd)) || ((ptr > gSysStart) && (ptr < gSysEnd)))
- {
- valid = true;
- }
- }
-
- return valid;
- }
-
- /*----------------------------------------------------------------------
- // TheModifiers
- //
- // Return the state of the modifier keys
- ----------------------------------------------------------------------*/
- short TheModifiers( )
- {
- EventRecord event;
-
- EventAvail( 0, &event );
- return( event.modifiers );
- }
-
- /*----------------------------------------------------------------------
- // OptionKeyIsDown
- //
- // Return true if the option key is down
- ----------------------------------------------------------------------*/
- Boolean OptionKeyIsDown( )
- {
- return( (TheModifiers() & optionKey) != 0 );
- }
-
- /*----------------------------------------------------------------------
- // CommandKeyIsDown
- //
- // Return true if the command key is down
- ----------------------------------------------------------------------*/
- Boolean CommandKeyIsDown( )
- {
- return( (TheModifiers() & cmdKey) != 0 );
- }
-
- /*----------------------------------------------------------------------
- // ShiftKeyIsDown
- //
- // Return true if the shift key is down
- ----------------------------------------------------------------------*/
- Boolean ShiftKeyIsDown( )
- {
- return( (TheModifiers() & shiftKey) != 0 );
- }
-
- /*----------------------------------------------------------------------
- // RgnToGlobal
- //
- // Convert a region from local coordinates to global coordinates
- ----------------------------------------------------------------------*/
- void RgnToGlobal( RgnHandle aRegion )
- {
- Point refPoint;
-
- refPoint.h = 0;
- refPoint.v = 0;
- LocalToGlobal(&refPoint);
- OffsetRgn( aRegion, refPoint.h, refPoint.v );
- }
-
- /*----------------------------------------------------------------------
- // RgnToLocal
- //
- // Convert a region from global coordinates to local coordinates
- ----------------------------------------------------------------------*/
- void RgnToLocal( RgnHandle aRegion )
- {
- Point refPoint;
-
- refPoint.h = 0;
- refPoint.v = 0;
- LocalToGlobal(&refPoint);
- OffsetRgn( aRegion, -refPoint.h, -refPoint.v );
- }
-
- /*----------------------------------------------------------------------
- // RectToGlobal
- //
- // Convert a rectangle from local coordinates to global coordinates
- ----------------------------------------------------------------------*/
- void RectToGlobal(Rect* aRect)
- {
- Point refPoint;
-
- refPoint.h = 0;
- refPoint.v = 0;
- LocalToGlobal(&refPoint);
- OffsetRect(aRect, refPoint.h, refPoint.v );
- }
-
- /*----------------------------------------------------------------------
- // RectToLocal
- //
- // Convert a rectangle from global coordinates to local coordinates
- ----------------------------------------------------------------------*/
- void RectToLocal(Rect* aRect)
- {
- Point refPoint;
-
- refPoint.h = 0;
- refPoint.v = 0;
- LocalToGlobal(&refPoint);
- OffsetRect(aRect, -refPoint.h, -refPoint.v );
- }
-
- /*----------------------------------------------------------------------
- // GetGlobalWindowLocation
- //
- // Return the location of a window in global coordinates
- ----------------------------------------------------------------------*/
- void GetGlobalWindowLocation(WindowPtr window, Rect* windowRect)
- {
- *windowRect = ((WindowPeek)window)->port.portRect;
- RectToGlobal(windowRect);
- }
-
- /*-----------------------------------------------------------------
- // ChangeCursor
- //
- // Change the shape of the cursor
- -----------------------------------------------------------------*/
- void ChangeCursor( short cursID )
- {
- CursHandle curs;
- short state;
-
- /*
- // Special checking: ID zero == arrow
- */
- if( !cursID )
- {
- SetCursor( &qd.arrow );
- return;
- }
- /*
- // Get the cursor resource & set its shape
- */
- curs = GetCursor(cursID);
- if( curs )
- {
- state = HGetState((Handle)curs);
- HLock((Handle)curs);
- SetCursor(*curs);
- HSetState((Handle)curs,state);
- }
- }
-